<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"/> 首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider/>

    <el-table :data="tableData" border style="width: 100%">
      <!--至少有一列设置宽度-->
      <el-table-column prop="id" label="ID" width="120" align="center"></el-table-column>
      <el-table-column label="头像" width="60" align="center">
        <template slot-scope="scope">
          <el-avatar :size="40" :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="120" header-align="center"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="120" header-align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号" width="110" header-align="center"></el-table-column>
      <el-table-column prop="email" label="电子邮箱" width="120" header-align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="description" label="简介" header-align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="是否启用" width="100" align="center">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ccc">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <!--添加绑定事件-->
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="small"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="small"
                     @click="openDeleteDialog(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改相册详情" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="editForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="相册简介" :label-width="formLabelWidth">
          <el-input v-model="editForm.description"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" :label-width="formLabelWidth">
          <el-input v-model="editForm.sort"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEidtForm()">修 改</el-button>
      </div>
    </el-dialog>
  </div>


</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      editForm: {
        id: '',
        username: '',
        nickname: '',
        phone:'',
        email:'',
        description:'',
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    openEditDialog(admin) {
      console.log("尝试打开编辑[id=" + admin.id + "]数据的对话框.....");
      let url = 'http://localhost:9081/admins/' + admin.id;
      console.log('url' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state === 2000) {
          this.editForm = responseBody.data;
          this.dialogFormVisible = true;
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              console.log(action)
              this.loadAdminList();
            }
          });
        }
      })
    },
    submitEidtForm() {
      let url = 'http://localhost:9081/admins/' + this.editForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm);
      this.axios.post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state === 2000) {
          this.dialogFormVisible = false;
          this.$message({
            message: '修改相册成功!',
            type: 'success'
          });
          this.loadAdminList();
        } else if (responseBody.state === 4040) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              console.log(action)
              this.loadAdminList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      })
    },
    openDeleteDialog(admin) {
      console.log("尝试打开删除[id=" + admin.id + "]数据的确认框.....")
      this.$confirm('此操作将永久删除[' + admin.username + ']管理员, 是否继续?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(admin);
      }).catch(() => {
      });
    },
    handleDelete(admin) {
      let jwt = localStorage.getItem('jwt');
      let url = 'http://localhost:9081/admins/' + admin.id + '/delete';
      this.axios
          .create({'headers': {'Authorization': jwt}})
          .post(url).then((response) => {
        let responseData = response.data;
        // 根据responseData中的状态码进行不同的处理
        if (responseData.state === 2000) {
          // 如果状态码为2000，调用loadAdminList方法，并显示成功消息
          this.loadAdminList();
          this.$message.success('删除成功!')
        } else if (responseData.state === 4040) {
          // 如果状态码为4040，弹出警告框显示responseData中的消息，并在确认后调用loadAdminList方法
          this.$alert(responseData.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              console.log(action)
              this.loadAdminList();
            }
          });
        } else {
          // 其他状态码，显示错误消息
          this.$message.error(responseData.message)
        }
      })
    },
    // 获取相册列表数据
    loadAdminList() {
      let jwt = localStorage.getItem('jwt');
      console.log("local storage jwt = " + jwt);
      // 定义后端接口
      let url = 'http://localhost:9081/admins';
      // 在控制台打印url的数值
      console.log("url=" + url);
      // 使用axios发送GET请求到url，并在获取响应后执行then方法
      this.axios
          .create({'headers': {'Authorization': jwt}})
          .get(url).then((response) => {
        // 从响应中获取数据赋值给responseData变量
        let responseData = response.data;
        // 如果responseData中的state属性值等于2000
        if (responseData.state === 2000) {
          // 将responseData中的data属性值赋给this.tableData
          this.tableData = responseData.data;
        }
      })
    }
  },
  // 生命周期方法
  mounted() {
    this.loadAdminList();
  }
}
</script>